package routes

import (
	v1 "ginblog/api/v1"
	"ginblog/middleware"
	"ginblog/utils"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
)

/*
	InitRoute  实现路由
*/
func InitRoute() *gin.Engine {
	gin.SetMode(utils.AppMode) //模式
	r := gin.New()
	r.Use(middleware.Cors())
	r.Use(middleware.GinLogger(zap.L()), middleware.GinRecovery(zap.L(), true))
	r.Use(gin.Recovery())
	// 注册自定义验证器函数
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("username", middleware.ValidateName)
		v.RegisterValidation("password", middleware.ValidatePassword)
		v.RegisterValidation("RoleValidator", middleware.RoleValidate)
	}
	auth := r.Group("/api/v1")
	auth.Use(middleware.JwtToken())
	{

		//user模块路由
		auth.GET("user/:id", v1.GetUserById)
		auth.PUT("user/:id", v1.UpdateUser)
		auth.DELETE("user/:id", v1.DelUser)
		//	article模块路由
		auth.POST("art/add", v1.AddArticle)
		auth.PUT("art/:id", v1.UpdateArt)
		auth.DELETE("art/:id", v1.DeleteArt)
		//	category路由
		auth.POST("category/add", v1.AddCategory)
		auth.PUT("category/:id", v1.UpdateCategory)
		auth.DELETE("category/:id", v1.DelCategory)
		//	上传文件
		auth.POST("uploadqn", v1.UploadQn)
		auth.POST("uploadali", v1.UploadAli)
	}
	//不需要认证
	router := r.Group("/api/v1")
	{
		router.POST("user/add", v1.AddUser)
		router.GET("users", v1.GetUserAll)
		router.GET("category", v1.GetCategory)
		router.GET("category/:id", v1.GetCategoryby)
		router.GET("art/:id", v1.GetArtInfo)
		router.GET("category/arts/:id", v1.GetCateArt)
		router.GET("arts", v1.GetArt)
		router.POST("login", v1.Login)
	}
	return r
}
